<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesResources;
use Illuminate\Support\Facades\Cache;
use App\Item;
use App\Configs;

class Controller extends BaseController
{
    public function __construct()
    {
        $setting = [];
        if(!Cache::has('setting')){
            $list = Configs::get();
            foreach($list as $item){
                $setting[$item->key] = $item->val;
            }
            Cache::forever('setting',$setting);
        } else {
            $setting = Cache::get('setting');
        }
        if(Cache::has('nav')){
            $navs = Cache::get('nav');
        }else{
            foreach(config('site.items') as $key=>$item){
                if(2 != $item['nav']){
                    $navs[$key]['name'] = $item['name'];
                    $navs[$key]['data'] = [];
                    if(1 == $item['nav']){
                        $navs[$key]['data'] = Item::where('type',$key)->select('id','name')->get();
                    }
                }
            }
            Cache::forever('nav',$navs);
        }
        view()->share('navs',$navs);
        view()->share('setting',$setting);
    }

    use AuthorizesRequests, AuthorizesResources, DispatchesJobs, ValidatesRequests;
}
